package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;

@Service
@Component
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    ItemMapper itemMapper;


    @Override
    public HomePageResponse homepage() {

    /*HomePageResponse homePageResponse=new HomePageResponse();
    homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
    homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return homePageResponse ;
    }*/

        HomePageResponse response = new HomePageResponse();
        Integer position = 0;
        Example example = new Example(Panel.class);
        example.createCriteria().andEqualTo("position",position);
        List<Panel> panels = null;
        try {
            panels = panelMapper.selectByExample(example);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
            return response;
        }
        HashSet<PanelDto> panelSet = new HashSet<>();
        for (Panel panel : panels) {
            PanelDto panelDto = contentConverter.panen2Dto(panel);
            Example panelContextExample = new Example(PanelContent.class);
            panelContextExample.createCriteria().andEqualTo("panelId", panel.getId());
            List<PanelContentItemDto> panelContentItemDtos = null;
            try {
                List<PanelContent> panelContents = panelContentMapper.selectByExample(panelContextExample);
                panelContentItemDtos = getPanelContentItemDtoList(panelContents);
            } catch (Exception e) {
                response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
                response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
                return response;
            }
            panelDto.setPanelContentItems(panelContentItemDtos);
            panelSet.add(panelDto);
        }
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        response.setPanelContentItemDtos(panelSet);
        return response;
    }

    /**
     * 根据List<PanelContent> 获得List<PanelContentItemDto>
     * @param panelContents
     * @return
     */
    private List<PanelContentItemDto> getPanelContentItemDtoList(List<PanelContent> panelContents) {
        List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContent2PaneContentItemDto(panelContents);
        for (PanelContentItemDto panelContentItemDto : panelContentItemDtos) {
            if (panelContentItemDto.getProductId() == null) {
                break;
            }
            Item item = itemMapper.selectByPrimaryKey(panelContentItemDto.getProductId());
            panelContentItemDto.setSalePrice(item.getPrice());
            panelContentItemDto.setProductName(item.getTitle());
            panelContentItemDto.setSubTitle(item.getSellPoint());
        }
        return panelContentItemDtos;
    }
}
